/*
 * Created by JFormDesigner on Sun Oct 19 17:53:09 CST 2025
 */

package com.yeban.javase.day10.swing_learn.other;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.List;
import java.util.Vector;

/**
 * @author yeban
 */
public class MyComboBox extends JFrame {

    public static void main(String[] args) {
        (new MyComboBox()).setVisible(true);
    }

    public MyComboBox() {
        initComponents();

        // 手动导包
        List<String> address = new ArrayList<String>();
        // 手动封装数据
        address.add("安徽淮北");
        address.add("安徽淮南");
        address.add("安徽六安");
        address.add("安徽合肥");

        // 封装下拉菜单的模型数据
        DefaultComboBoxModel model = new DefaultComboBoxModel(new Vector(address));
        comboBox1.setModel(model);
    }

    private void comboBox1(ActionEvent e) {
        // TODO add your code here
        int index = comboBox1.getSelectedIndex();
        System.out.println("index = " + index);
        String string = comboBox1.getSelectedItem().toString();
        System.out.println("string = " + string);

        // 设置文本域内容
        textField1.setText(string);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        comboBox1 = new JComboBox<>();
        textField1 = new JTextField();
        label1 = new JLabel();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- comboBox1 ----
        comboBox1.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u6c5f\u82cf\u65e0\u9521",
            "\u5b89\u5fbd\u6dee\u5317",
            "\u6cb3\u5357\u90d1\u5dde",
            "\u5e7f\u4e1c\u5e7f\u5dde"
        }));
        comboBox1.addActionListener(e -> comboBox1(e));
        contentPane.add(comboBox1);
        comboBox1.setBounds(new Rectangle(new Point(130, 95), comboBox1.getPreferredSize()));
        contentPane.add(textField1);
        textField1.setBounds(130, 140, 100, 35);

        //---- label1 ----
        label1.setText("\u5bb6\u5ead\u4f4f\u5740");
        contentPane.add(label1);
        label1.setBounds(65, 88, 75, 40);

        contentPane.setPreferredSize(new Dimension(400, 300));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JComboBox<String> comboBox1;
    private JTextField textField1;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
